package com.chainyoung.component_tmtrade.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chainyoung.common.mvp.BasePresenter;
import com.chainyoung.common.utils.ConvertUtils;
import com.chainyoung.common.utils.ScreenUtils;
import com.chainyoung.common.utils.StringUtil;
import com.chainyoung.common.view.CommonPopupWindow;
import com.chainyoung.component_tmtrade.R;
import com.chainyoung.component_tmtrade.R2;
import com.chainyoung.component_tmtrade.bean.TradeCurrencyItem;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class TradeAgencyAdapter extends BaseQuickAdapter<TradeCurrencyItem, BaseViewHolder> {

    private OnClickCancel onClickCancel;
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    public TradeAgencyAdapter(@Nullable List<TradeCurrencyItem> data) {
        super(R.layout.tmtrade_list_agency_item1, data);
    }


    @Override
    protected void convert(final BaseViewHolder helper, final TradeCurrencyItem item) {

        helper.setText(R.id.tv_type, item.getType() == 1 ? R.string.buy_in : R.string.sell_out);
        helper.setTextColor(R.id.tv_type, item.getType() == 1 ? ContextCompat.getColor(mContext, R.color._09C173) : ContextCompat.getColor(mContext, R.color._F95A54));
        ((TextView) helper.getView(R.id.tv_type)).setCompoundDrawablesRelativeWithIntrinsicBounds(item.getType() == 1 ?
                R.drawable.shape_trade_type_mark_green : R.drawable.shape_trade_type_mark_red, 0, 0, 0);
        ((TextView) helper.getView(R.id.tv_type)).setCompoundDrawablePadding(10);
        helper.setText(R.id.tv_time, sdf.format(new Date(item.getAddtime() * 1000)));

        helper.setText(R.id.tv_price,new BigDecimal(item.getPrice()).setScale(6, BigDecimal.ROUND_DOWN).stripTrailingZeros().toPlainString());
        helper.setText(R.id.tv_total, new BigDecimal(item.getNum()).setScale(6, BigDecimal.ROUND_DOWN).stripTrailingZeros().toPlainString());
        helper.setText(R.id.tv_already,new BigDecimal(item.getDeal()).setScale(6, BigDecimal.ROUND_DOWN).stripTrailingZeros().toPlainString());

        helper.setText(R.id.txt_price,String.format(mContext.getString(R.string.tmtrade_price_bit), item.getXnb()));
        helper.setText(R.id.txt_total,String.format(mContext.getString(R.string.tmtrade_amount_bit), item.getScb()));
        helper.setText(R.id.txt_already,String.format(mContext.getString(R.string.tmtrade_actual_deal_bit), item.getScb()));



      /*  helper.getView(R.id.tv_more).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View mPopView = LayoutInflater.from(mContext).inflate(R.layout.layoyut_cancel_exchange_window, null);
                final CommonPopupWindow popupWindow = new CommonPopupWindow.Builder(mContext)
                        .setView(mPopView)
                        .setBackGroundLevel(1.0f)
                        .setOutsideTouchable(true)
                        .setWidthAndHeight(ConvertUtils.dp2px(mContext, 120), ViewGroup.LayoutParams.WRAP_CONTENT)
                        .create();
                popupWindow.showAsDropDown(helper.getView(R.id.tv_more), ConvertUtils.dp2px(mContext, -100), ConvertUtils.dp2px(mContext, -60));
                if (mPopView != null) {
                    mPopView.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (onClickCancel != null) {
                                onClickCancel.onClickCancel(helper.getAdapterPosition(), item.getId());
                                popupWindow.dismiss();
                            }
                        }
                    });
                }


            }
        });*/
    }

    public void setOnClickCancel(OnClickCancel onClickCancel) {
        this.onClickCancel = onClickCancel;
    }

    public interface OnClickCancel {
        void onClickCancel(int position, String id);
    }


}

